package com.erp.module.jiJian

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.widget.ArrayAdapter
import com.erp.activity.BaseActivity
import com.erp.bean.Twostringbean
import com.erp.R
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import kotlinx.android.synthetic.main.activity_jijianguolv.*
import kotlinx.android.synthetic.main.header_knoya.*
import java.util.*

/**
 * Created by Administrator on 2017/2/24/024.
 * 作者：刘洋
 */

class JiJianGuoLvActivity : BaseActivity(), View.OnClickListener {
    private val shijianleixing = arrayOf("计件时间")
    private val shijianvolue = arrayOf("pw.start_time")
    private val gongxu = arrayOf("全部工序", "开料", "封厚边", "封薄边", "排钻", "分拣")
    private val gongxuvolue = arrayOf("0", "1", "2", "3", "4", "5")
    lateinit var searchParams: ArrayList<Twostringbean>
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_jijianguolv
    }

    override fun initCreate() {
        iv_header_left.visibility = View.VISIBLE
        tv_header_title.text = "计件过滤"
        val shijianleibieadapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, shijianleixing)
        val gongxuadapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, gongxu)
        SpIstime.adapter = shijianleibieadapter
        SpGongxu.adapter = gongxuadapter
        BtQueding.setOnClickListener(this)
        iv_header_right.isFocusable = true
        iv_header_right.isFocusableInTouchMode = true
        iv_header_right.requestFocus()
        iv_header_right.requestFocusFromTouch()
        searchParams=ArrayList()
        TvStartTime.setOnClickListener(this)
        TvEndTime.setOnClickListener(this)
        BtQueding.setOnClickListener(this)
        TvQingkong.setOnClickListener(this)
        iv_header_left.setOnClickListener(this)
        initData()
    }

    private fun initData() {
        //        tvStartTime.setText(Myutil.getjulidate(-10));
        //        tvEndTime.setText(Myutil.getjulidate(1));
        val bundle = intent.getParcelableExtra<Bundle>("searchParams")
        val s = bundle.getParcelableArrayList<Twostringbean>("searchParams")
        for (i in s.indices) {
            val t = s[i]
            when (t.name) {
                NAME -> EtName.setText(t.string)
                NUMBLE -> EtGonghao!!.setText(t.string)
                GONGXU -> for (j in gongxuvolue.indices) {
                    if (t.string == gongxuvolue[j]) {
                        SpGongxu.setSelection(j)
                        break
                    }
                }
                TIMELEI -> for (j in shijianvolue.indices) {
                    if (t.string == shijianvolue[j]) {
                        SpIstime!!.setSelection(j)
                        break
                    }
                }
                STARTTIME -> TvStartTime!!.text = t.string
                ENDTIME -> TvEndTime!!.text = t.string
            }
        }
    }

    override fun onClick(v: View) {
        when (v) {
            iv_header_left -> {
                finish()
                overridePendingTransition(0, R.anim.end_activity1)
            }
            TvQingkong -> {
                val end=Myutil.getjulidate1(1) + " 00:00:00"
                TvEndTime.text = end
                val start = Myutil.getjulidate1(-10) + " 00:00:00"
                TvStartTime.text = start
                EtGonghao.setText("")
                EtName.setText("")
                SpIstime.setSelection(0)
                SpGongxu.setSelection(0)
            }
            TvStartTime -> {
                Myutil.notjianpan(this@JiJianGuoLvActivity, iv_header_right)
                Dialogutil.showBottoPopupWindow(TvStartTime, "选择开始时间", TvStartTime!!.text.toString(), this@JiJianGuoLvActivity)
            }
            TvEndTime -> {
                Myutil.notjianpan(this@JiJianGuoLvActivity, iv_header_right)
                Dialogutil.showBottoPopupWindow(TvEndTime, "选择结束时间", TvEndTime!!.text.toString(), this@JiJianGuoLvActivity)
            }
            BtQueding -> {
                searchParams = ArrayList()
                if (!Myutil.isettrue(EtName)) {
                    addsearchParams(NAME, EtName!!.text.toString(), searchParams)
                }
                if (!Myutil.isettrue(EtGonghao)) {
                    addsearchParams(NUMBLE, EtGonghao.text.toString(), searchParams)
                }
                for (i in gongxu.indices) {
                    if (SpGongxu.selectedItem.toString() == gongxu[i]) {
                        addsearchParams(GONGXU, gongxuvolue[i], searchParams)
                        break
                    }
                }
                for (i in shijianleixing.indices) {
                    if (SpIstime.selectedItem.toString() == shijianleixing[i]) {
                        addsearchParams(TIMELEI, shijianvolue[i], searchParams)
                        break
                    }
                }
                if (!Myutil.istvtrue(TvStartTime)) {
                    addsearchParams(STARTTIME, TvStartTime.text.toString(), searchParams)
                }
                if (!Myutil.istvtrue(TvEndTime)) {
                    addsearchParams(ENDTIME, TvEndTime.text.toString(), searchParams)
                }
                val intent = Intent()
                val bundle = Bundle()
                bundle.putParcelableArrayList("searchParams", searchParams)
                intent.putExtra("searchParams", bundle)
                setResult(Activity.RESULT_OK, intent)
                finish()
                overridePendingTransition(0, R.anim.end_activity1)
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == 4) {
            finish()
            overridePendingTransition(0, R.anim.end_activity1)
        }
        return super.onKeyDown(keyCode, event)
    }

    companion object {
        val NAME = "searchParams[wu_name]"                            //名字
        val NUMBLE = "searchParams[work_number]"                      //工号
        val GONGXU = "searchParams[pw_ws]"                            //工序
        val TIMELEI = "searchParams[time_filter][time_category]"      //时间类型
        val STARTTIME = "searchParams[time_filter][start_time]"       //开始时间
        val ENDTIME = "searchParams[time_filter][end_time]"           //结束时间
        private val a = "JiJianGuoLvActivity"

        fun addsearchParams(name: String, volue: String, searchParams: MutableList<Twostringbean>) {
            val twostringbean = Twostringbean()
            twostringbean.name = name
            twostringbean.string = volue
            searchParams.add(twostringbean)
        }
    }
}
